package cn.donting.jme.swing;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.ArrayList;

/**
 * @see JDTabbedPane
 */
@Getter
@Setter
@Slf4j
public abstract class Tab extends JPanel {
    private String title;
    private Icon icon;
    private String tip;
    private JTabbedPane jTabbedPane;
    private ArrayList<ChangeListener> activateChangeListeners = new ArrayList<>();

    public Tab() {
        setTitle("");
    }

    public boolean isShowClosable() {
        return false;
    }

    protected void close() {

    }

    public void setLoading() {

    }

    public void setLoadError(Exception e) {

    }

    public void setTip(String tip) {
        this.tip = tip;
        if (jTabbedPane != null) {
            int i = jTabbedPane.indexOfComponent(this);
            jTabbedPane.setToolTipTextAt(i, tip);
        }
    }

    public void setIcon(Icon icon) {
        this.icon = icon;
        if (jTabbedPane != null) {
            int i = jTabbedPane.indexOfComponent(this);
            jTabbedPane.setIconAt(i, icon);
        }
    }

    public void setTitle(String title) {
        this.title = title;
        if (jTabbedPane != null) {
            int i = jTabbedPane.indexOfComponent(this);
            jTabbedPane.setTitleAt(i, title);
        }
    }

    public void setjTabbedPane(JTabbedPane jTabbedPane) {
        this.jTabbedPane = jTabbedPane;
    }

    /**
     * 激活转态改变
     *
     * @param activate
     */
    protected void activateChange(boolean activate) {
        log.info("activate {}:{}", getTitle(), activate);
    }
}
